﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace Thinktecture.UIAnnotations.Behaviors
{
    public class DefaultButtonBehavior : TargetedTriggerAction<FrameworkElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            var window = this.AssociatedObject as FrameworkElement;

#if SILVERLIGHT
            window.KeyUp += this.OnKeyUp;
            
            // set the default button border
            if (this.TargetObject != null && this.TargetObject is Button)
            {
                var button = this.TargetObject as Button;
                var border = new Border()
                {
                    BorderBrush = new SolidColorBrush(Colors.Black),
                    BorderThickness = new Thickness(1,1,1,1.5),
                    CornerRadius = new CornerRadius(3),
                    Margin = button.Margin
                };
                button.Margin = new Thickness(-1, -1, 0, 0);
                border.SetValue(Grid.RowProperty, button.GetValue(Grid.RowProperty));
                border.SetValue(Grid.RowSpanProperty, button.GetValue(Grid.RowSpanProperty));
                border.SetValue(Grid.ColumnProperty, button.GetValue(Grid.ColumnProperty));
                border.SetValue(Grid.ColumnSpanProperty, button.GetValue(Grid.ColumnSpanProperty));
                var panel = button.Parent as Panel;
                if (panel != null)
                {
                    var index = panel.Children.IndexOf(button);
                    panel.Children.Remove(button);
                    panel.Children.Insert(index, border);
                    border.Child = button;
                }
            }
#else
            // set the IsDefault property (in case of WPF)
            if (this.TargetObject != null && this.TargetObject is Button)
            {
                var button = this.TargetObject as Button;
                button.IsDefault = true;
            }
#endif
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
#if SILVERLIGHT
            var window = this.AssociatedObject as FrameworkElement;
            window.KeyUp -= this.OnKeyUp;
#endif
        }

#if SILVERLIGHT
        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && this.TargetObject != null && this.TargetObject is Button)
            {
                var button = this.TargetObject as Button;
                var peer = new ButtonAutomationPeer(button);
                var provider = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                provider.Invoke();
            }
        }
#endif

        protected override void Invoke(object parameter)
        {
        }

    }
}
